package com.dz51.module.systemparameter.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.dz51.core.log4j.ILog4jManager;
import com.dz51.module.systemparameter.service.ISystemParameterService;

/**
 * @Title: InitSystemParameterCacheListener.java
 * @Package com.dz51.module.systemparameter.listener
 * @Description: 系统参数缓存初始化listener
 * @author Zhaoqt
 * @date 2014-5-10 上午4:57:18
 * @version V1.0
 */
public class InitSystemParameterCacheListener implements ServletContextListener {

	public void contextDestroyed(ServletContextEvent arg0) {

	}

	/**
	 * 同步系统全部参数到缓存
	 */
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		// 获取servlet上下文
		ServletContext servletContext = servletContextEvent.getServletContext();
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		// 获取系统参数服务层
		ISystemParameterService systemParameterService = (ISystemParameterService) wac
				.getBean("com.dz51.module.systemparameter.service.impl.SystemParameterServiceImpl");
		ILog4jManager log4jManager = (ILog4jManager) wac.getBean("log4jManager");
		try {
			systemParameterService.synSystemParameterCacheAll(null);
		} catch (Exception e) {
			e.printStackTrace();
			log4jManager.saveExceptionLog(getClass().getName(), "synSystemParameterCacheAll", "异常发生在[" + getClass()
					+ "]类方法[contextInitialized],异常信息[" + e.getMessage() + "]", e);
		}

	}

}
